package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.entity.SysUser;
import com.ktjy.entity.vo.SysUserVo;
import com.ktjy.mapper.SysRoleMapper;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuqiaomiao
 * @since 2024-03-01
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUserVo getUserByUsrNameAndPwd(String usrName, String usrPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        queryWrapper.eq("usr_password", usrPassword);
        SysUser temp = getOne(queryWrapper);
        if (temp != null) {
            SysUserVo result = new SysUserVo();
            BeanUtils.copyProperties(temp, result);
            result.setRole(sysRoleMapper.selectById(temp.getUsrRoleId()));
            return result;
        }
        return null;
    }

    @Override
    public SysUserVo getUserByUsrName(String usrName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        SysUser temp = getOne(queryWrapper);
        if (temp != null) {
            SysUserVo result = new SysUserVo();
            BeanUtils.copyProperties(temp, result);
            result.setRole(sysRoleMapper.selectById(temp.getUsrRoleId()));
            return result;
        }
        return null;
    }

    @Override
    public Page pageRecords(Page page, QueryWrapper queryWrapper) {

        return sysUserMapper.pageList(page,queryWrapper);
    }
}
